package com.example.web.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.utils.Result;
import com.example.web.system.entity.CheckMenuTreeVo;
import com.example.web.system.entity.SaveMenuIdsByRoleIdParam;
import com.example.web.system.entity.SysRole;
import com.example.web.system.entity.SysRoleParam;
import com.example.web.system.service.impl.SysMenuServiceImpl;
import com.example.web.system.service.impl.SysRoleServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kimido
 * @since 2024-07-03
 */
@RestController
@RequestMapping("/api/role")
@Api(tags = "角色管理")
public class SysRoleController {

    @Autowired
    private SysRoleServiceImpl sysRoleService;

    @Autowired
    private SysMenuServiceImpl sysMenuService;
    @ApiOperation("获取分配权限菜单")
    @GetMapping("/getAssignTree/{roleId}")
    public Result getAssignTree(@PathVariable("roleId") Integer roleId){
         CheckMenuTreeVo checkMenuTreeVo=sysMenuService.getAssignTree(roleId);
         return Result.success(checkMenuTreeVo);
    }

    @ApiOperation("保存角色拥有的权限信息")
    @PutMapping("/saveMenuIdsByRoleId")
    public Result saveMenuIdsByRoleId(@RequestBody SaveMenuIdsByRoleIdParam param){
        sysMenuService.saveMenuIdsByRoleId(param);
        return Result.success();
    }

    @ApiOperation("角色列表")
    @GetMapping("/list")
    public Result list(SysRoleParam sysRoleParam){
        IPage<SysRole> page =sysRoleService.list(sysRoleParam);
        return Result.success(page);
    }

    @ApiOperation("添加角色")
    @PostMapping("/add")
    public Result addRole(@RequestBody SysRole sysRole){
        sysRole.setDeleted(1);
        boolean save = sysRoleService.save(sysRole);
        if(save){
            return Result.success();
        }
        return Result.error(500,"添加失败");
    }
    @ApiOperation("修改角色")
    @PostMapping("/update")
    public Result updateRole(@RequestBody SysRole sysRole){
        boolean b = sysRoleService.updateById(sysRole);
        if(b){
            return Result.success();
        }
        return  Result.error(500,"修改失败");
    }

    @ApiOperation("删除角色")
    @DeleteMapping("/delete/{roleId}")
    public Result deleteRole(@PathVariable("roleId") Integer roleId){
        boolean b = sysRoleService.removeById(roleId);
        if(b){
            return Result.success();
        }
        return Result.error(500,"删除失败");
    }


}
